<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Session;
use think\Paginator;
use think\Controller;

class User extends Admin
{

    // 查询数据(查询正常运行的管理员)
    public function index(Request $request)
    {
        // 查询数控
        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['name'] = ['like', '%'.$data.'%'];

            $count = Db::table('user')->where($where)->paginate(5)->count();

            if ($count == 0) {

                 $this->success('未找到查询的内容');
            }

        }else{

            $where = "";

        }

        // 查询数据
        $data = Db::table('user')->field(true)->where('status',1)->where($where)->paginate(5);

        //查询首分类
        
        //获取分页显示
        $page = $data->render();

        // 渲染模板
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('User/userList');


    }

    // 查询数据(查询已禁用的管理员)
    public function disable(Request $request)
    {
        // 查询数控
        $res = $request->get();

        if (!empty($res['title'])) {

            $data = $res['title'];

            $where['name'] = ['like', '%'.$data.'%'];

            $count = Db::table('user')->where($where)->paginate(5)->count();

            if ($count == 0) {

                 $this->success('未找到查询的内容');
            }

        }else{

            $where = "";

        }

        // 查询数据
        $data = Db::table('user')->field(true)->where('status',2)->where($where)->paginate(5);

        //查询首分类
        
        //获取分页显示
        $page = $data->render();

        // 渲染模板
        $this->assign('page', $page);
        $this->assign('data', $data);

        // 返回
        return $this->fetch('User/disable');

    }


    // 新增保存数据
    public function save(Request $req)
    {

        // 接收数据
        $data = $req->get();
        // var_dump($data);die;

        $res = Db::table('user')->where('name', $data['name'])->find();
        $tel = Db::table('user')->where('tel', $data['tel'])->find();

        if(!empty($res)){

            $info['status'] = false;
            $info['msg'] = '名字已存在';

            return json($info);

        }

        if(!empty($tel)){

            $info['status'] = false;
            $info['msg'] = '电话号码已存在';

            return json($info);

        }

        // 密码加密
        $data['pass'] = md5($data['pass']);

        // 执行sql
        $result = Db::table('user')->data($data)->insert();

        // 返回结果
        if ($result > 0) {
           
            $info['status'] = true;
            $info['msg'] = '保存成功';

            return json($info);

        } else {

                      
            $info['status'] = false;
            $info['msg'] = '保存失败';

            return json($info);
        }

    }


    // 查询语句
    public function find($id)
    {

        // 查询
        $data = Db::table('user')->field(['userpass'],true)->find($id);
        // var_dump($data);die;

        // 跳转
        
        // return view('admin@user/edit', ['data' => $data]);

        if ($data === null) {

            $info['status'] = false;
            // $info['title'] = '查无数据';
            $info['data'] = '查无数据,请重试...';

            return json($info);

        } else {

            $info['status'] = true;
            // $info['title'] = $data['name'];
            $info['data'] = $data;

            return json($info);

        }

    }


    // 验证原密码
    public function matchpass(Request $request)
    {
        // 查询数控
        $data = $request->get();

        // var_dump($data);die;

        // $user = Db::table('user')->where('id',$data['id'])->find();


        if (md5($data['ypass']) == $data['hpass']) {

            $info['data'] = '<font color="green">&nbsp;&nbsp;密码正确!</font>';

            return json($info);

        }else{

            $info['data'] = '<font color="red">&nbsp;&nbsp;原密码错误</font>';

            return json($info);
        }


    }

    // 修改管理员资料
    public function updata(Request $request)
    {
        // 是否为 Ajax 请求
        if (!Request::instance()->isAjax()) {

            $info['status'] = false;
            // $info['title'] = '查无数据';
            $info['data'] = '你迷路吧??';

            return json($info);
        }

        // 接收数据
        $data = $request->get();

        $user = Db::table('user')->where('id',$data['id'])->select();

        // 获取id

        unset($data['ypass']);
        unset($data['repass']);


        // 加密
        $data['pass'] = md5($data['pass']);

        // 执行sql语句
        $result = Db::table('user')->where('id', $data['id'])->update($data);

        // 返回结果
        if ($result > 0) {
           
            $info['status'] = true;
            $info['msg'] = '保存成功';

            return json($info);

        } else {

                      
            $info['status'] = false;
            $info['msg'] = '保存失败';

            return json($info);
        }
    }



    // 跳转分配角色页面
    public function role($id)
    {
        // 查询角色数据
        $sql = DB::table('role')
                ->field(['create_time'],true)
                ->where('status = 1')
                ->select();

        $data = DB::table('user_role')
                ->where('uid',$id)
                ->select();
        foreach ($sql as $k => $v) {
            $sql[$k]['check'] = null;
            foreach ($data as $y) {
                if ($y['role_id'] == $v['id']) {
                    $sql[$k]['check'] = 'checked';
                }
            }
        }
        // var_dump($sql);

        return view('admin@user/role',['data' => $sql,'uid'=>$id]);


    }


     // 提交分配角色
    public function doRole(Request $request)
    {

        // 接收数据
        $data = $request->post();
        $uid = $data['uid'];
        $a['uid'] = $uid;
        if ($data !== $a) {
            $rid = $data['role_id'];
            Db::table('user_role')->where('uid',$uid)->delete();

            foreach ($rid as $v) {
                $data = [
                    'role_id' => $v,
                    'uid' => $uid,
                ];
            //插入数据
            $res = DB::table('user_role')->data($data)->insert();

            }
        }else{
            Db::table('user_role')->where('uid',$uid)->delete();

            return $this->success('角色已分配',url('admin/User/index'));
        }

        // 返回结果
        if ($res > 0) {

             return $this->success('角色已分配!', url('admin/User/index'));

        } else {

             return $this->error('角色分配失败,请重试...');

        }

    }

    // 管理员禁用
    public function status()
    {
        $id = input('id');

        // 查询
        $data = DB::table('user')->find($id);

        if ($data['name'] == '王川平') {

            $info['status'] = false;
            $info['msg'] = '想禁用"超管",除非你是董事长...';

            return json($info);

        }

        // 改变值
        $data['status'] = $data['status'] == 2?'1':'2';

        $res = $data['status'] == 1?'已启用':'已禁用';



        // 插入
        $sql = DB::table('user')->where('id',$id)->update($data);



        // 返回结果
        if ($sql > 0) {

            
            $info['status'] = true;
            $info['msg'] = $res;

            return json($info);

        } else {

                    
            $info['status'] = false;
            $info['msg'] = '操作失败';

            return json($info);


        }

    }

}


